package com.pulse.base.service.impl;

import com.pulse.base.service.ICtrlMsgService;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @ClassName CtrlMsgServiceImpl
 * @Description
 * @Author 陆帅
 * @Date 2024/7/16 10:38
 */
@Service("CtrlMsgService")
public class CtrlMsgServiceImpl implements ICtrlMsgService {

    public final static Logger log = LoggerFactory.getLogger(CtrlMsgServiceImpl.class);

    private ExecutorService pool = Executors.newFixedThreadPool(200);


    /**
     * 数据处理
     *
     * @param topic
     * @param message
     */
    @Override
    public void msgHandler(String topic, MqttMessage message) {
        String[] topicArr = topic.split("/");
        if (topicArr.length != 4) {
            log.error("异常topic:" + topic);
            return;
        }
        try {
            pool.execute(new CtrlMsgThread(topicArr, topic, message));
        } catch (Exception e) {
            log.error("异常：", e);
            log.error("处理mqtt消息(data)异常：" + e + "\r" + e.getStackTrace());
        }
    }
}
